package entity_controllers.fieldcontrollers;

import boundary.UI;
import ie.FieldBoard;
import ie.PlayerBoard;
import ie.Shaker;
import controllers.FieldController;
import core.subcontrollers.PlayerController;
import core.subcontrollers.Rules;
import dto.Field;
import dto.Player;
import dto.PropertyField;
import entity.Account;
import entity.fields.properties.Street;
import entity_controllers.options.effects.JailEffect;

public abstract class PropertyController extends FieldController {
		
	@Override
	public void execute(Player player, Field field, FieldBoard fieldBoard, PlayerBoard playerBoard, Shaker shaker) {
		
		PropertyField property = (PropertyField) field;

		Player owner = ((PropertyField) field).getOwner();
		
		if(owner == null){
			buyField(player, property, fieldBoard.getFields());
		}
		else if(owner != player)
			if(!((Account) owner.getAccount()).getEM().getOption(JailEffect.class).isEnabled())
				payRent(player, property);

		UI.get().showPlayers(playerBoard.getPlayers());
		UI.get().showFields(fieldBoard.getFields());
		
	}
	
	private boolean buyField(Player buyer, PropertyField property, Field[] fields) {
		int price = property.getPrice();
		if(UI.get().getBoolean("Vil du købe "+property.getName()+" for "+price+"?", "Køb", "Nej")){
			if(PlayerController.payAmount(buyer, null, price)){
				property.setOwner(buyer);
				buyer.getAccount().addProperty(property);

				return true;
			}
			else{
				UI.get().getMessage("Du har ikke råd til at købe "+property.getName());
			}
		}
		return false;
	}
	
	private void payRent(Player player, PropertyField property){
		int amount = this.getRent(property);
		
		if(!PlayerController.payAmount(player, property.getOwner(), amount)){
			UI.get().getMessage("Du kan ikke betale "+amount+" i leje");
			Rules.setBankrupt(player, property.getOwner());
		}
			
	}
	
	abstract public int getRent(PropertyField property);
	
}
